﻿using He.Common;
using He.Web.UI;
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.Security;

namespace He.Web.tools
{
    /// <summary>
    /// user_ajax 的摘要说明
    /// </summary>
    public class user_ajax : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            string act = InputUtil.Get("act");

            switch (act)
            {
                case "login":
                    Login(context);
                    break;
                case "status":
                    GetStatus(context);
                    break;
                case "exit":
                    Exit(context);
                    break;
            }
        }

        #region 会员登录===================================
        private void Login(HttpContext context)
        {
            string username = InputUtil.Get("username");
            string userpwd = InputUtil.Get("userpwd");
            if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(userpwd))
            {
                JsonString.EchoError("账号和密码均不能为空");
            }
            BLL.member mgr = new BLL.member();
            if (mgr.Exists(username, CryptoHelper.DESEncrypt(userpwd)))    //登录成功
            {
                Model.member member = mgr.GetModel(username);
                if (member.status == (int)Enums.MemberStatus.人工审核)
                {
                    JsonString.EchoError("您的帐号尚未通过审核，需审核通过后才能登录");
                }
                if (member.status == (int)Enums.MemberStatus.冻结)
                {
                    JsonString.EchoError("您的帐号已被冻结，请联系管理员！");
                }
                else
                {
                    //登录处理
                    member.last_time = member.this_time;
                    member.this_time = DateTime.Now.ToString();
                    member.last_ip = member.this_ip;
                    member.this_ip = context.Request.UserHostAddress;
                    mgr.Update(member);

                    CookieHelper.Set("username", username);

                    //设置验证Cookie并跳转
                    FormsAuthentication.SetAuthCookie(username, false);
                    //FormsAuthentication.RedirectFromLoginPage(username, false);
                    JsonString.EchoSuccess("登录成功", "/user/center.aspx");
                }
            }
            else
            {
                JsonString.EchoError("用户名或密码错误，请核对后重新输入");
            }
        }
        #endregion

        #region 会员状态===================================
        private void GetStatus(HttpContext context)
        {
            StringBuilder strTemp = new StringBuilder();
            if (!context.Request.IsAuthenticated)
            {
                strTemp.Append("<div class=\"middle\">");
                strTemp.Append(" <input id=\"username\" type=\"text\" value=\"用户名\" class=\"username\" onfocus=\"if(this.value=='用户名'){this.value=''}\"  onblur=\"if(this.value==''){this.value='用户名'}\" />");
                strTemp.Append(" <span>密码</span><input id=\"userpwd\" type=\"password\" onkeyup=\"login(event)\" value=\"\" class=\"password\" onfocus=\"if($(this).prev().text()=='密码'){$(this).prev().text('')}\"  onblur=\"if(this.value==''){$(this).prev().text('密码')}\" />");
                strTemp.Append("<input id=\"btnLogin\" src=\"/images/login.jpg\" type=\"image\" class=\"img\" onmouseover=\"$(this).attr('src','/images/login_hover.jpg')\" onmouseout=\"$(this).attr('src','/images/login.jpg')\"/>");
                strTemp.Append("</div>");
                strTemp.Append("<div class=\"right\">");
                strTemp.Append("<a href=\"/user/login.aspx\">注册</a> <a href=\"/user/pwd_find.aspx\">忘记密码</a></div>");
            }
            else
            {
                Model.member member = new BLL.member().GetModel(context.User.Identity.Name);
                strTemp.Append("<div class=\"middle\">");
                strTemp.AppendFormat("您好 {0},欢迎来到BJ姐妹汇！｜<a href='/user/center.aspx'>我的账户</a>｜<a href='/tools/user.ashx?act=exit'>退出登录", member.user_name);
                strTemp.Append("</div>");
            }
            context.Response.Write(strTemp.ToString());
        }
        #endregion

        #region 退出登录===================================
        private void Exit(HttpContext context)
        {
            FormsAuthentication.SignOut();
            string url = context.Request.UrlReferrer != null ? context.Request.UrlReferrer.ToString() : FormsAuthentication.LoginUrl;

            context.Response.Redirect(url);
        }
        #endregion


        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}